package com.tlx.core.util;

import java.io.File;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tlx.util.ConvertStringUtils;

//特殊格式重命名
public class CustomFilenameUtils {

    private static final Logger logger = LoggerFactory.getLogger(CustomFilenameUtils.class);

    public static void main(String[] args) {
        simpleRename("D:\\王煜全");
    }

    public static void simpleRename(String dir) {

        File[] picList = new File(dir).listFiles();
        for (File file : picList) {
            try {
                String oldName = file.getName();
                String newName = oldName;
                newName = ConvertStringUtils.normalConvert(newName);

                // 字符串是否与正则表达式相匹配
                if (newName.indexOf(" ") == 4) {
                    newName = newName.substring(0, 4) + newName.substring(5, newName.length());
                }
                if (newName.indexOf("-") == 4) {
                    newName = newName.substring(0, 4) + newName.substring(5, newName.length());
                }

                System.out.println(newName);
                if (!StringUtils.equals(newName, oldName)) {
                    boolean b = file.renameTo(new File(file.getParent(), newName));
                    String msg = b ? "成功" : "失败";
                    logger.info("【" + msg + "】【" + oldName + "】  ---> 【" + newName + "】");
                }
            } catch (Exception e) {
                System.out.println("出错了：" + file.getAbsolutePath());
                e.printStackTrace();

            }
        }
    }

    public static void youkuRename(String dir, int numCount) {

        File[] picList = new File(dir).listFiles();
        for (File file : picList) {

            try {
                String newName = file.getName();
                // 处理无效字符开始
                newName = StringUtils.remove(newName, "_超清");
                newName = StringUtils.remove(newName, "_高清");
                newName = StringUtils.remove(newName, "_标清");

                String ext = FilenameUtils.getExtension(newName);
                String name = FilenameUtils.getBaseName(newName);
                String realName = name.substring(0, name.lastIndexOf(" "));
                String numName = name.substring(name.lastIndexOf(" ") + 1, name.length());
                numName = StringUtils.leftPad(numName, numCount, "0");
                newName = numName + "_" + realName;

                while (newName.endsWith("-")) {
                    newName = newName.substring(0, newName.length() - 1);
                }
                // 处理无效字符完毕

                newName = newName + "." + ext;
                System.out.println(newName);
                file.renameTo(new File(file.getParent(), newName));
            } catch (Exception e) {
                System.out.println("出错了：" + file.getAbsolutePath());
                e.printStackTrace();

            }
        }
    }

}
